---
description:
  Generate GraphQL schema from MySQL database with GraphQL Mesh. Learn how to use the MySQL handler
  and its powerful 'where' field.
---

import { Callout } from '@theguild/components'

# MySQL

This handler allows you to generate GraphQL schema from an existing MySQL database.

## How to use?

To get started, install the handler library:

```sh npm2yarn
npm i @omnigraph/mysql
```

Now, you can use it directly in your Mesh config file:

```ts filename="mesh.config.ts" {2,7-9}
import { defineConfig } from '@graphql-mesh/compose-cli'
import { loadMySQLSubgraph } from '@omnigraph/mysql'

export const composeConfig = defineConfig({
  subgraphs: [
    {
      sourceHandler: loadMySQLSubgraph('Employees', {
        endpoint: 'mysql://root:passwd@localhost:3306/employees'
        // You can use environment variables like
        // endpoint: "mysql://{env.MYSQL_USER}:{env.MYSQL_PASSWORD}@{env.MYSQL_HOST}:{env.MYSQL_PORT}/{env.MYSQL_DATABASE}"
      })
    }
  ]
})
```

### How does `where` work?

Every CRUD operation has `where` field in its input, so you can see all the columns of a table.
`where` works like below;

```graphql
{
  getProduct(
    where: {
      id: 5
      year: ">2010"
      price: "100..200"
      level: "<=3"
      sn: "*str?"
      label: "str"
      code: "(1,2,4,10,11)"
    }
  ) {
    id
    name
  }
}
```

This GraphQL operation will send the following query to your MySQL database;

```sql
SELECT id, name FROM product WHERE id = 5 AND year > '2010' AND (price BETWEEN '100' AND '200') AND level <= '3' AND sn LIKE '%str\_' AND label = 'str' AND code IN (1,2,4,10,11)
```

<Callout>
  [You can find a working example here](
  https://github.com/ardatan/graphql-mesh/tree/main/examples/v1-next/mysql-rfam )
</Callout>
